package jsyf.oa.service.doc;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.doc.TypeDao;
import jsyf.oa.entity.doc.TypeEntity;
import jsyf.oa.facade.doc.TypeFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/20.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class TypeService implements TypeFacade{

    @Autowired
    private TypeDao typeDao;


    @Override
    public String save(String jsonData) {
        TypeEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), TypeEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = typeDao.add(entity);
        } else {
            TypeEntity typeEntity = (TypeEntity) typeDao.getIBaseEntityById(entity.getId());
            typeEntity.setName(entity.getName());
            typeEntity.setUrl(entity.getUrl());
            typeEntity.setDelFlag(entity.getDelFlag());
            typeEntity.setSortNo(entity.getSortNo());
            rows = typeDao.update(typeEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String jsonData) {
        return typeDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public List<JSONObject> getTypeList() {
        List<JSONObject> typeList =
                typeDao.getJSONListByCondition("AND DEL_FLAG = 0 ",new Object[]{});
        return typeList;
    }

    @Override
    public JSONObject getTypeListByPage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String name = jsonParams.getString("name");
        String url = jsonParams.getString("url");
        String key = jsonParams.getString("key");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(name)) {
            condition.append(" AND name = ?");
            sqlArgs.add(name);
        }
        if (!StringUtils.isBlank(url)) {
            condition.append(" AND url = ?");
            sqlArgs.add(url);
        }
        if (!StringUtils.isBlank(key)) {
            condition.append(" AND name like ?");
            sqlArgs.add("%" + key + "%");
        }
        condition.append(" AND DEL_FLAG = 0");
        condition.append(" ORDER BY SORT_NO DESC");
        //查询list
        List<JSONObject> records = typeDao.getJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = typeDao.getPageCount(condition.toString(),sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        //子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getTypeById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        JSONObject result = typeDao.getJSONById(id);
        return result;
    }

}